import type {
  BasicVxeTableProps,
  VxeGridInstance,
  ModalMethods,
  VxeFormItemProps,
  VxeGridPropTypes,
} from '@mxpio/components';
import { useModal } from '@mxpio/components';
import { useMessage, usePermission, useProfile } from '@mxpio/hooks';
import type { VxeTableQueryParams } from '@mxpio/utils/src/criteria';
import { getVxeTableQueryParams } from '@mxpio/utils/src/criteria';
import { onMounted, reactive, ref, Ref, watch } from 'vue';
// 新增必要的类型导入
import type { Recordable, VxeToolbarPropTypes, VxeToolbarProps } from '@mxpio/types';
// 定义返回类型接口
interface UseListCrudReturn {
  auth: {
    add: string;
    edit: string;
    delete: string;
    import: string;
  };
  tableRef: Ref<VxeGridInstance | undefined>;
  gridOptions: BasicVxeTableProps;
  registerModal: (instance: ModalMethods, uuid: number) => void;
  openModal: <T = any>(props?: boolean, data?: T, openOnSet?: boolean) => void;
  handleAdd: () => void;
  handleEdit: (record: Recordable) => void;
  handleDetail: (record: Recordable) => void;
  handleDelete: (record: Recordable) => void;
  handleBatchDel: () => void;
  handleSuccess: () => void;
  checkboxChange: () => void;
  configData: Ref<Recordable>;
}

// 定义参数类型接口
interface UseListCrudOptions {
  componentName: string;
  columns: VxeGridPropTypes.Columns;
  searchFormSchema?: VxeFormItemProps[];
  pageApi?: (params: Recordable) => Promise<any>;
  deleteApi?: (ids: string) => Promise<any>;
  configApi?: () => Promise<any>;
  authConfig?: {
    add?: string;
    edit?: string;
    delete?: string;
    import?: string;
    export?: string;
  };
  tableId?: string;
  tableMinHeight?: number;
  rowConfig?: BasicVxeTableProps['rowConfig'];
  toolbarConfig?: BasicVxeTableProps['toolbarConfig'];
  formConfig?: BasicVxeTableProps['formConfig'];
  customConfig?: BasicVxeTableProps['customConfig'];
  proxyConfig?: BasicVxeTableProps['proxyConfig'];
  pagerConfig?: BasicVxeTableProps['pagerConfig'];
  expandConfig?: BasicVxeTableProps['expandConfig'];
  sortConfig?: BasicVxeTableProps['sortConfig'];
  module?: string; // 模块字段
  filters?: Recordable | (() => Recordable); // 过滤器字段
  importCode?: string; // 导入模版编码字段
  addBefore?: () => Recordable | Promise<Recordable>;
  editBefore?: (params: Recordable) => Recordable | Promise<any>;
  deleteBefore?: (params: Recordable | Recordable[]) => Recordable | Promise<any>;
  importBefore?: (params: Recordable) => Recordable | Promise<any>;
  exportBefore?: (params: Recordable) => Recordable | Promise<any>;
  deleteAfter?: (row: any, res: any) => void;
  loadBefore?: (params: {
    page?: VxeTableQueryParams['page'];
    form?: VxeTableQueryParams['form'];
    sorts?: VxeTableQueryParams['sorts'];
    filters?: Recordable;
  }) => Recordable;
  // 新增：追加自定义按钮配置
  appendButtons?: VxeToolbarPropTypes.Buttons;
}

export function useListCrud(options: UseListCrudOptions): UseListCrudReturn {
  const {
    componentName,
    columns,
    searchFormSchema,
    pageApi,
    deleteApi,
    configApi,
    authConfig,
    tableId = `${componentName}Table`,
    tableMinHeight = 700,
    rowConfig = {},
    toolbarConfig = {},
    formConfig = {},
    customConfig = {},
    proxyConfig = {},
    pagerConfig = {},
    expandConfig = {},
    sortConfig = {},
    module = 'sys',
    filters = {},
    addBefore,
    editBefore,
    deleteBefore,
    deleteAfter,
    importCode,
    loadBefore,
    appendButtons = [],
  } = options;

  // 权限控制
  const auth = reactive({
    add: authConfig?.add || `${module}:${componentName}:add`,
    edit: authConfig?.edit || `${module}:${componentName}:edit`,
    delete: authConfig?.delete || `${module}:${componentName}:delete`,
    import: authConfig?.import || `${module}:${componentName}:import`,
    export: authConfig?.export || `${module}:${componentName}:export`,
  });

  // 状态管理
  const selectedRowCount = ref(0);
  const _importCode = ref('');
  const tableRef = ref<VxeGridInstance>();
  const configData = ref({});

  // 模态框
  const [registerModal, { openModal }] = useModal();

  // 工具函数
  const { restoreStore, updateStore } = useProfile();
  const { hasPermission } = usePermission();
  const { createMessage, createConfirm } = useMessage();

  const rowConfigDefault = {
    keyField: 'id',
  };

  const addBtn = {
    content: '新增',
    buttonRender: {
      name: 'AButton',
      props: {
        type: 'primary',
        preIcon: 'mdi:page-next-outline',
      },
      attrs: {
        class: 'ml-2',
      },
      events: {
        click: handleAdd,
      },
    },
    visible: hasPermission(auth.add),
  };

  const delBtn = {
    content: '删除',
    code: 'delete',
    buttonRender: {
      name: 'AButton',
      props: {
        type: 'primary',
        danger: true,
        preIcon: 'mdi:delete-forever',
      },
      events: {
        click: handleBatchDel,
      },
    },
    visible: hasPermission(auth.delete) && selectedRowCount.value > 0,
  };

  const exportTool = {
    toolRender: {
      name: 'ExportButton',
      attrs: {
        class: 'ml-2',
      },
      props: {
        export: auth.export,
      },
    },
  };

  const importTool = {
    toolRender: {
      name: 'ImportButton',
      props: () => {
        return { importCode: importCode || _importCode.value };
      },
    },
    visible: hasPermission(auth.import),
  };
  const toolbarConfigDefault: VxeToolbarProps = {
    buttons: [],
    tools: [],
  };
  // 工具栏配置
  if (!toolbarConfig || toolbarConfig.export !== false) {
    toolbarConfigDefault.tools?.push(exportTool);
  }
  if (!toolbarConfig || toolbarConfig.import !== false) {
    toolbarConfigDefault.tools?.push(importTool);
  }

  const formConfigDefault = {
    enabled: searchFormSchema ? true : false,
    items: searchFormSchema,
  };

  const customConfigDefault = {
    storage: {
      visible: true,
      resizable: true,
      sort: true,
      fixed: true,
    },
    restoreStore: restoreStore,
    updateStore: updateStore,
  };

  const proxyConfigDefault = {
    ajax: {
      query: async ({ page, form, sorts }: VxeTableQueryParams) => {
        return loadData({ page, form, sorts });
      },
    },
  };

  const pagerConfigDefault = {
    pageSizes: [10, 20, 100, 200],
    pageSize: 10,
    autoHidden: false,
    size: 'small',
  };

  const sortConfigDefault: BasicVxeTableProps['sortConfig'] = {
    trigger: 'cell',
    remote: true,
    defaultSort: {
      field: 'createTime',
      order: 'desc',
    },
  };

  // 表格配置
  const gridOptions = reactive<BasicVxeTableProps>({
    id: tableId,
    columns: columns,
    rowConfig: Object.assign(rowConfigDefault, rowConfig),
    // toolbarConfig: Object.assign(toolbarConfigDefault, toolbarConfig),
    toolbarConfig: {
      ...toolbarConfigDefault,
      ...toolbarConfig,
      buttons: toolbarConfig.buttons ? toolbarConfig.buttons : [addBtn, ...appendButtons, delBtn],
      // toolbarConfig.export ? toolbarConfig.export : exportTool,
      // toolbarConfig.import ? toolbarConfig.import : importTool,
      tools: toolbarConfig.tools ? toolbarConfig.tools : toolbarConfigDefault.tools,
    },
    formConfig: Object.assign(formConfigDefault, formConfig),
    customConfig: Object.assign(customConfigDefault, customConfig),
    minHeight: tableMinHeight,
    proxyConfig: Object.assign(proxyConfigDefault, proxyConfig),
    pagerConfig: Object.assign(pagerConfigDefault, pagerConfig),
    sortConfig: Object.assign(sortConfigDefault, sortConfig),
    expandConfig: expandConfig,
  });

  // 加载数据
  async function loadData({ page, form, sorts }: VxeTableQueryParams) {
    // 当无排序参数时，使用默认排序
    const _sorts: VxeGridPropTypes.ProxyAjaxQuerySortCheckedParams[] | undefined =
      sorts?.length === 0
        ? ([
            ...(Array.isArray(sortConfig?.defaultSort)
              ? sortConfig.defaultSort
              : [sortConfig?.defaultSort || sortConfigDefault?.defaultSort]),
          ] as VxeGridPropTypes.ProxyAjaxQuerySortCheckedParams[])
        : sorts;
    // 解析 filters：如果是函数则执行，否则直接使用对象
    const resolvedFilters = typeof filters === 'function' ? filters() : filters;
    const params = loadBefore
      ? loadBefore({
          page,
          form,
          sorts: _sorts,
          filters: resolvedFilters,
        })
      : getVxeTableQueryParams({
          page,
          form,
          sorts: _sorts,
          filters: resolvedFilters,
        });
    const res = await pageApi?.(params);
    if (!res.first && res.last && res.content.length === 0) {
      setTimeout(() => {
        tableRef.value?.commitProxy('query');
      }, 1000);
    }
    return res;
  }

  // 打开创建模态框
  async function handleAdd() {
    try {
      let extendData;
      if (addBefore && typeof addBefore === 'function') {
        extendData = await addBefore();
      }
      openModal(true, {
        isUpdate: false,
        extendData: extendData,
      });
    } catch (error) {
      console.error('Failed to fetch config:', error);
    }
  }

  // 打开编辑模态框
  async function handleEdit(record: Recordable) {
    try {
      let extendData;
      if (editBefore && typeof editBefore === 'function') {
        extendData = await editBefore(record);
      }
      openModal(true, {
        record,
        isUpdate: true,
        extendData,
      });
    } catch (error) {
      console.error('Failed to fetch config:', error);
    }
  }

  async function handleDetail(record: Recordable) {
    try {
      openModal(true, {
        record,
        isUpdate: true,
        disabled: true,
      });
    } catch (error) {
      console.error('Failed to fetch config:', error);
    }
  }

  // 单行删除
  async function handleDelete(record: Recordable) {
    try {
      if (deleteBefore && typeof deleteBefore === 'function') {
        await deleteBefore(record);
      }
      const keyField = gridOptions.rowConfig?.keyField || rowConfigDefault.keyField;
      const id = record[keyField];
      const res = await deleteApi?.(id).then(() => {
        tableRef.value?.commitProxy('query');
      });
      // createMessage.success('删除成功');
      if (deleteAfter && typeof deleteAfter === 'function') {
        deleteAfter(record, res);
      }
      selectedRowCount.value = 0;
    } catch (error) {
      console.error('Failed to fetch config:', error);
    }
  }

  // 批量删除
  async function handleBatchDel() {
    try {
      const selectedRows: Recordable[] = tableRef.value?.getCheckboxRecords() || [];
      if (selectedRows.length === 0) {
        createMessage.error('请选择要删除的数据');
        return;
      }
      if (deleteBefore && typeof deleteBefore === 'function') {
        await deleteBefore(selectedRows);
      }
      createConfirm({
        title: '是否确认删除',
        iconType: 'warning',
        centered: false,
        onOk: async () => {
          const keyField = gridOptions.rowConfig?.keyField || rowConfigDefault.keyField;
          const selectedRowKeys = selectedRows.map((item) => item[keyField]);
          const res = await deleteApi?.(selectedRowKeys.join()).then(() => {
            tableRef.value?.commitProxy('query');
          });
          // createMessage.success('删除成功');
          if (deleteAfter && typeof deleteAfter === 'function') {
            deleteAfter(selectedRows, res);
          }
          selectedRowCount.value = 0;
        },
      });
    } catch (error) {
      console.error('Failed to fetch config:', error);
    }
  }

  // 复选框变化事件
  function checkboxChange() {
    const selectedRows: Recordable[] = tableRef.value?.getCheckboxRecords() || [];
    selectedRowCount.value = selectedRows.length;
  }

  // 操作成功后的回调
  async function handleSuccess() {
    tableRef.value?.commitProxy('query');
  }

  watch(selectedRowCount, () => {
    const deleteButton = gridOptions?.toolbarConfig?.buttons?.find((btn) => btn.code === 'delete');
    if (deleteButton) {
      deleteButton.visible = hasPermission(auth.delete) && selectedRowCount.value > 0;
    }
  });

  // 初始化加载配置
  onMounted(async () => {
    if (configApi) {
      try {
        const res = await configApi();
        _importCode.value = res?.importTemplate;
        configData.value = res;
      } catch (error) {
        console.error('Failed to fetch config:', error);
      }
    }
  });

  return {
    auth,
    configData,
    tableRef,
    gridOptions,
    registerModal,
    handleAdd,
    handleEdit,
    handleDetail,
    handleDelete,
    handleBatchDel,
    handleSuccess,
    checkboxChange,
    openModal,
  };
}
